﻿using ComicBookApp.Data.DataAdapters.Adapters;
using ComicBookApp.Data.DataAdapters.Interfaces;
using ComicBookApp.DataModels;
using ComicBookApp.Web.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace ComicBookApp.Web.Controllers
{
    public class CharacterController : Controller
    {
        IComicBookAppAdapter _adapter;

        public CharacterController()
        {
            _adapter = new ComicBookAppAdapter();
        }
        public CharacterController(ComicBookAppAdapter adapter)
        {
            _adapter = adapter;
        }
        // GET: Character
        public ActionResult Index()
        {
            List<Character> characters = _adapter.GetAllCharacters();
            return View(characters);
        }
        public ActionResult Add(int id)
        {
            ComicBook comic = _adapter.GetComicBook(id);

            return View(comic);
        }
        [HttpPost]
        public ActionResult Add(Character character)
        {
            character = _adapter.CreateCharacter(character);
            return RedirectToAction("Detail/" + character.Id);
        }

        public ActionResult Detail(int id = 0)
        {
            if (id <= 0)
            {
                return RedirectToAction("Index");
            }
            CharacterDetailVM bucket = new CharacterDetailVM();

            bucket.Character = _adapter.GetCharacter(id);
            bucket.ComicBook = _adapter.GetComicBook(bucket.Character.ComicBookId);

            return View(bucket);
        }

        public ActionResult Edit(int id = 0)
        {
            if (id <= 0)
            {
                return RedirectToAction("Index");
            }
            CharacterDetailVM bucket = new CharacterDetailVM();

            bucket.Character = _adapter.GetCharacter(id);
            bucket.ComicBook = _adapter.GetComicBook(bucket.Character.ComicBookId);

            return View(bucket);
        }

        [HttpPost]
        public ActionResult Edit(Character character)
        {
            character = _adapter.UpdateCharacter(character);

            return RedirectToAction("Detail/" + character.Id);
        }

        [HttpPost]
        public ActionResult Delete(int id)
        {
            Character character = _adapter.DeleteCharacter(id);

            return RedirectToAction("Detail/" + character.ComicBookId, "ComicBook");
        }
        
    }
}